using Microsoft.AspNetCore.Mvc;
using Universal.Application.Contracts.Common;
using Universal.Application.Contracts.Dto.Auth;
using Universal.Application.Repositories;

namespace Universal.Api.Controllers;

[ApiController]
[Route("api/[controller]")]
public class AuthController : ControllerBase
{

    private readonly IAuthService _authService;
    public AuthController(IAuthService authService)
    {
        _authService = authService;
    }

    [HttpPost("register")]
    public async Task<IActionResult> Register([FromBody] RegisterDto registerDto)
    {

        // 检查模型绑定和数据注解校验是否全部通过
        if (!ModelState.IsValid)
        {
            return Ok(ApiResponse<string>.Failure(400, "参数校验失败"));
        }

        var result = await _authService.RegisterAsync(registerDto);
        return Ok(result);
    }

    [HttpPost("login")]
    public async Task<IActionResult> Login([FromBody] LoginDto loginDto)
    {
        var result = await _authService.LoginAsync(loginDto);
        return Ok(result);
    }

    [HttpPost("refresh")]
    public async Task<IActionResult> Refresh([FromBody] RefreshTokenRequestDto refreshTokenRequestDto)
    {
        var result = await _authService.RefreshTokenAsync(refreshTokenRequestDto);
        return Ok(result);
    }



}